var doing = false;
var server;
var cur;
var shopId;
var staticServer;

$(document).ready(function(){
	
	server = $("#server").val();
	shopId = $("#shopId").val();
	staticServer = $("#staticServer").val();
	
	loaded();
	
	switchTag();
});

function loaded(){
    var myScroll;
	myScroll = new IScroll('#wrapper',{
		probeType: 2,//probeType：1对性能没有影响。在滚动事件被触发时，滚动轴是不是忙着做它的东西。probeType：2总执行滚动，除了势头，反弹过程中的事件。这类似于原生的onscroll事件。probeType：3发出的滚动事件与到的像素精度。注意，滚动被迫requestAnimationFrame（即：useTransition：假）。  
        scrollbars: false,//有滚动条  
        mouseWheel: true,//允许滑轮滚动  
        fadeScrollbars: false,//滚动时显示滚动条，默认影藏，并且是淡出淡入效果  
        bounce:true,//边界反弹  
        interactiveScrollbars:true,//滚动条可以拖动  
        shrinkScrollbars:'scale',// 当滚动边界之外的滚动条是由少量的收缩。'clip' or 'scale'.  
        click: true ,// 允许点击事件  
        keyBindings:true,//允许使用按键控制  
       // disableMouse: true,
        momentum:true// 允许有惯性滑动  
	});
	
	myScroll.on('scrollEnd', function loadItem(){
		var y = myScroll.maxScrollY;
		var cy = myScroll.y;
		
		if((y - cy) > -230){
			if(doing){ 
	    		return;
	    	}
	    	doing=true;
	    	
	    	var page = Number($("#currentPage").val())+1;
	    	$.ajax({
	    	    type: 'POST',
	    	    url:server+"/merchandise/search/"+shopId+"/"+page,
	    	    dataType: "text",
	    	    async:false,
	    	    success:function(result) {
	    	    	
	    	    	if(result == null || result == "" || result == undefined){
	    	    		return;
	    	    	}
	    	    	
	    	    	result = result.replace(/\"merchandiseNumber\":(\d+)/g,"\"merchandiseNumber\":\"$1\"");
	    	    	
	    	    	var merchandiseList = JSON.parse(result);
	    	    	var tags = "";
	    	    	for (var i = 0; i < merchandiseList.length; i++) {
	    	    		var merchandise = merchandiseList[i];
	    	    		var merchandiseNumber = merchandise.merchandiseNumber;
	    	    		var merchandiseIcon = merchandise.merchandiseIcon;
	    	    		var merchandiseNameCn = merchandise.merchandiseNameCn;
	    	    		if(merchandiseNameCn.length>=17){
	    	    			merchandiseNameCn = merchandiseNameCn.substring(0,15)+"...";
	    	    		}
	    	    		var merchandiseSpec = merchandise.merchandiseSpec;
	    	    		var merchandiseDomesticPrice = merchandise.merchandiseDomesticPrice;
	    	    		var merchandisePrice = merchandise.merchandisePrice;
	    	    		
	    	    		var tag = "<li><div class='pImgS'>";
	    	    		tag += "<a href="+server+"/merchandise/"+merchandiseNumber+".html>";
	    	    		tag += "<img src="+staticServer+merchandiseIcon+"></a>";
	    	    		tag += "</div>";
	    				
	    	    		tag += "<div class='pDetail'>";
	    	    		tag += "<a href="+server+"/merchandise/"+merchandiseNumber+".html>";
	    	    		tag += "<p class='pTitle'>"+merchandiseNameCn+"</p>";
	    	    		tag += "<p class='pML'>"+merchandiseSpec+"</p></a>";
	    	    		tag += "<div class='suggestP fr'><label class='insidePrice'>国内价&nbsp;";
	    	    		tag += "<span>￥"+merchandiseDomesticPrice+"</span></label>";
	    	    		tag += "<label class='J_singleTotalA'>店内价<span class='countSingleA redFont'>€"+merchandisePrice.toFixed(2)+"</span>";
	    				tag += "</label></div></div></li>";
	    				
	    				tags += tag;
	    			}
	    	    	$("#J_productList").append(tags);
	    	    	$("#currentPage").val(page);
	    	    	myScroll.refresh();
	    	    }
	    	});
	    	
	    	doing=false; 
		}
	});
	
	document.addEventListener('DOMContentLoaded',loaded, false);
}

function loadItem(){
	var page = $("currentPage").val()+1;
	$.ajax({
	    type: 'POST',
	    url:server+"/merchandise/search/"+shopId+"/"+page,
	    dataType: "text",
	    success:function(result) {
	    	
	    	if(result == null || result == "" || result == undefined){
	    		return;
	    	}
	    	
	    	var merchandiseList = JSON.parse(result);
	    	var tags = "";
	    	for (var i = 0; i < merchandiseList.length; i++) {
	    		var merchandise = merchandiseList[i];
	    		var merchandiseNumber = merchandise.merchandiseNumber;
	    		var merchandiseIcon = merchandise.merchandiseIcon;
	    		var merchandiseNameCn = merchandise.merchandiseNameCn;
	    		if(merchandiseNameCn.length>=17){
	    			merchandiseNameCn = merchandiseNameCn.substring(0,15)+"...";
	    		}
	    		
	    		var merchandiseSpec = merchandise.merchandiseSpec;
	    		var merchandiseDomesticPrice = merchandise.merchandiseDomesticPrice;
	    		var merchandisePrice = merchandise.merchandisePrice;
	    		
	    		var tag = '<li><div class="pImgS">';
	    		tag += '<a href='+staticServer+'"/merchandise/'+merchandiseNumber+'.html">';
	    		tag += '<img src='+staticServer+'"'+merchandiseIcon+'"></a>';
	    		tag += '</div>';
				
	    		tag += '<div class="pDetail">';
	    		tag += '<a href='+staticServer+'"/merchandise/'+merchandiseNumber+'.html">';
	    		tag += '<p class="pTitle">'+merchandiseNameCn+'</p>';
	    		tag += '<p class="pML">'+merchandiseSpec+'</p></a>';
	    		tag += '<div class="suggestP fr"><label class="insidePrice">国内价&nbsp;';
	    		tag += '<span>￥'+merchandiseDomesticPrice+'</span></label>';
	    		tag += '店内价<span class="countSingleA redFont">€'+merchandisePrice+'</span>';
				tag += '</label></div></div></li>';
				
				tags += tag;
			}
	    	$("#J_productList").append(tags);
	    	$("currentPage").val(page);
	    }
	});
}

function switchTag(){
	$("#J_tabComponent").find(".tabList li").on('click',function (){
		$(this).siblings("li").removeClass("selected");
		$(this).addClass("selected");
		_id = $(this).find("a").attr("href");
		$(this).parents(".tabListBox").siblings("div").hide();
		$(this).parents(".tabListBox").siblings(_id).show();
		var scrollId = "#"+$(_id).find(".J_wrapper").attr("id");
		 $("#J_productList li").live("click tap",function (e){
			        _href=$(this).find('a').attr('href');
			        if(_href != null){
			            window.location.href = _href;
			        }
			    }) 
	})
}
